import { baseUrl } from '../../../../utils/config'
const moment = require('../../../../utils/moment.js')
const { post, get } = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPageContainer: true,
    checked: false,
    show: false,
    duration: 300,
    position: 'bottom',
    round: true,
    overlay: true,
    birthday: '0000年00月00日',
    showInviteBind: true,
    owner: 1,
    baseUrl: baseUrl,
    parentsName: '',
    headImagePath: 'user.png',
    parent: {
      headPicPath: 'user.png',
      memberName: "",
      birthday: "1990-01-01",
      bindWx: false,
      accountCode: ""
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ parentsName, id }) {
    const owner = wx.getStorageSync('owner');
    console.log(owner);

    if (id > 0) {
      get(`family/get/${id}`, null, (data) => {
        data.birthday = moment(data.birthdayStr, 'YYYY年MM月DD日').format('YYYY-MM-DD');
        this.setData({
          showInviteBind: data.owner ? true : false,
          parent: data,
          owner: data.owner,
          birthday: data.birthdayStr,
          parentsName: parentsName,
          headImagePath: data.headPicPath
        })
      })
    } else {
      this.setData({
        parentsName: parentsName,
        showInviteBind: owner == 1 ? true : false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

    wx.updateShareMenu({
      withShareTicket: true,
      isPrivateMessage: true,
      activityId: '监听页面加载',
    })

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.setData({
      showPageContainer: false
    })
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    prevPage.showPageContainer();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const unicode = wx.getStorageSync('unicode')
    return {
      "title": `邀请${this.data.parentsName}加入`,
      "path": `pages/tabBar/summary/home?owner=0&unicode=${unicode}`,
      "imageUrl": "/resoures/logo.png"
    }
  },
  chooseHeadPic() {
    const headType = this.data.parentsName == "爸爸" ? "father" : "mother";
    wx.navigateTo({
      url: `/pages/me/family/pickHeadImg/pickHeadImg?headType=${headType}`,
    })
  },
  bindDateChange({ detail: { value } }) {
    this.setData({
      date: value
    })
  },
  cancel() {
    wx.navigateBack();
  },
  confirm() {
    const unicode = wx.getStorageSync('unicode');

    if (this.data.headImagePath == "user.png") {
      wx.showToast({
        title: '请选择头像',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    if (this.data.parent.memberName == "") {
      wx.showToast({
        title: '请输入姓名',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    if (this.data.birthday == "0000年00月00日") {
      wx.showToast({
        title: '请填写出生日期',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    if (this.data.parent.bindWx == 0) {
      wx.showToast({
        title: '请绑定微信',
        icon: 'error',
        duration: 2000
      });
      return;
    }

    const owner = wx.getStorageSync('owner');

    const params = {
      memberName: this.data.parent.memberName,
      memberAlias: this.data.parentsName,
      sex: this.data.parentsName == "爸爸" ? "男" : "女",
      birthday: moment(this.data.parent.birthday),
      headPicPath: this.data.headImagePath,
      bindWx: this.data.parent.bindWx,
      owner: owner != undefined ? owner : this.data.owner,
      unicode: unicode
    }

    if (this.data.parent.id) {
      params.id = this.data.parent.id
    }

    get('family/save', params, () => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000,
        success: function () {
          setTimeout(() => {
            const pages = getCurrentPages();
            const prevPage = pages[pages.length - 2];
            prevPage.freshChild();
            wx.navigateBack();
          }, 2000);
        }
      });
    })

  },
  setBindWx() {
    this.data.parent.bindWx = 1
    this.setData({
      parent: this.data.parent
    })
  },
  bindWxAccount() {
    this.setData({
      show: true,
      checked: false
    })
  },
  bindDateChange({ detail: { value } }) {
    const formateDate = moment(value).format('YYYY年MM月DD日');
    this.setData({
      birthday: formateDate
    })
  },

  clickPrivacy() {
    wx.navigateTo({
      url: '/pages/me/privacy/privacy',
    })
  },

  changeCheckedPrivacy() {
    this.setData({
      checked: !this.data.checked
    })
  },
  cancelBind() {
    this.setData({
      show: false
    })
  },
  confimAddReply() {
    if (!this.data.checked) {
      wx.showToast({
        title: '请先同意用户协议',
        icon: 'error',
        duration: 2000,
      })
    } else {
      this.setData({
        show: false
      })
      this.setBindWx();
    }
  }


})